package com.jwdf.uda.view.dialog;

import android.content.Context;
import android.content.DialogInterface;
import android.databinding.DataBindingUtil;
import android.graphics.drawable.AnimationDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import com.jwdf.uda.R;
import com.jwdf.uda.bean.UploadImage;
import com.jwdf.uda.databinding.DialogAddCmtBinding;
import com.jwdf.uda.event.DialogDismissEvent;
import com.jwdf.uda.utils.DisplayUtils;
import com.jwdf.uda.utils.EventBusUtil;
import com.jwdf.uda.utils.StringUtils;
import java.util.List;


/**
 * Created by Administrator on 2016/10/18.
 */
public class CommentDialog extends BaseDialog {
    private DialogAddCmtBinding mBinding;
    private boolean mCancelable;
    private CallbackListener mListener;
    private AnimationDrawable mAnimDrawable;
    private int mAtCmtId = -1;
    private String mAtNickName = "";

    public CommentDialog(Context context) {
        super(context, R.style.input_dialog_style);
        setCancelable(false);
        setCanceledOnTouchOutside(false);
        setCanBackDismiss(true);
    }

    public CommentDialog(Context context, CallbackListener listener) {
        this(context);
        this.mListener = listener;

    }

    public CommentDialog(Context context, int atCommentId, String atNickname, CallbackListener listener) {
        this(context, listener);
        this.mAtCmtId = atCommentId;
        this.mAtNickName = atNickname;
    }

    public void setCanBackDismiss(boolean canBackDismiss) {
        this.mCancelable = canBackDismiss;
    }

    @Override
    public void show() {
        super.show();
        //设置大小
        WindowManager.LayoutParams lp = this.getWindow().getAttributes();
        lp.width = DisplayUtils.getDeviceWidth(getContext());
        lp.height = (DisplayUtils.getDeviceHeight(getContext()) * 3) / 4;
        this.getWindow().setAttributes(lp);

        Window window = getWindow();
        window.setWindowAnimations(R.style.botton_animation);
        window.setGravity(Gravity.BOTTOM);
    }

    @Override
    protected View onInflateView(LayoutInflater inflater) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.dialog_add_cmt, null, false);
        return mBinding.getRoot();
    }

    @Override
    protected void onInitView(View view) {
        final Context context = view.getContext();
        mAnimDrawable = (AnimationDrawable) mBinding.ivLoad.getBackground();
        mBinding.tvCmtTitle.setText(mAtCmtId > 0 ? context.getString(R.string.title_comment_at, mAtNickName) : context.getString(R.string.title_comment));
        mBinding.ivCmtClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        mBinding.ivCmtSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onCmtSubmit(mAtCmtId);
                }
            }
        });
        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                EventBusUtil.post(new DialogDismissEvent());
            }
        });
    }

    public void showProgressView() {
        mBinding.progressviewLayout.setVisibility(View.VISIBLE);
        mAnimDrawable.start();
    }

    public void closeProgressView() {
        if (isProgressViewShowing() && mAnimDrawable.isRunning()) {
            mBinding.progressviewLayout.setVisibility(View.GONE);
            mAnimDrawable.stop();
        }
    }

    private boolean isProgressViewShowing() {
        return View.VISIBLE == mBinding.progressviewLayout.getVisibility();
    }

    public String getContent() {
        String content = mBinding.etContent.getText().toString();
        return StringUtils.filterNullAndTrim(content);
    }

    public List<UploadImage> getUploadImageList() {
        return mBinding.isImages.getUploadImageList();
    }

    public void setImageToken(UploadImage uploadImage) {
        mBinding.isImages.setImageToken(uploadImage);
    }

    @Override
    public void onBackPressed() {
        if (mCancelable) {
            super.onBackPressed();
        }
    }

    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        EventBusUtil.registerEvent(mBinding.isImages);
    }

    @Override
    public void onDetachedFromWindow() {
        EventBusUtil.unregisterEvent(mBinding.isImages);
        super.onDetachedFromWindow();
    }

    public interface CallbackListener {
        void onCmtSubmit(int atCommentId);
    }

}
